//
//  THJContractDetailVC2.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/9/3.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJContractDetailVC2.h"

@interface THJContractDetailVC2 ()<UIWebViewDelegate>
{
    UIWebView * webview ;
}

@end

@implementation THJContractDetailVC2

- (void)viewDidLoad {
    [super viewDidLoad];
    [self SVHUDShow];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"合同详情"];
    }else{
        [self setNavBarWithTitle:@"合同详情"];
    }
    webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, NavbarHeight, ScreenWidth, ScreenHeight - NavbarHeight)];
    [self.view addSubview:webview];
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"contract2.html" withExtension:nil];
    webview.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
    [webview loadRequest:request];
    // Do any additional setup after loading the view from its nib.
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self getData];
}
-(void)getData{
    
    NSDictionary * dic = @{@"id":[RSA encryptString:self.orderId publicKey:RSA_PubKey],@"type":@"ios"};
    NSLog(@"tijiaoLL-%@",dic);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=hetong_sell_info" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"卖家合同信息--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
            [self setContractWithDic:receiveData[@"data"]];
        }else{
            [self HUDShowMsg:@"获取信息失败" AfterDelayDimiss:1];
        }
    }];
}
-(void)setContractWithDic:(NSDictionary*)dic{
    NSArray * arr = @[
                      @"buyer_name",
                      @"buyer_address",
                      @"buyer_corp",
                      @"buyer_deputy",
                      @"buyer_mobile",
                      @"buyer_account",
                      @"buyer_bank",
                      @"buyer_tax_number",
                      @"c_buyer",
                      @"c_seller",
                      @"c_tax",
                      @"c_pay_type",
                      @"c_gg",
                      @"c_delivery_type",
                      @"c_pack",
                      @"c_amount",
                      @"c_total_money",
                      @"c_notes",
                      @"c_standard",
                      @"c_penalty",
                      @"c_unit",
                      @"c_total_money_upper",
                      @"c_product_name",
                      @"c_date",
                      @"c_number",
                      @"seller_name",
                      @"seller_address",
                      @"seller_corp",
                      @"seller_deputy",
                      @"seller_mobile",
                      @"seller_account",
                      @"seller_bank",
                      @"seller_tax_number",
                      @"c_freight_pay"
                      ];
    NSArray * arr2 = @[
                       [self DCRP_STR:dic[@"buyer_truename"]],
                       [self DCRP_STR:dic[@"buyer_address"]],
                       [self DCRP_STR:dic[@"buyer_faren"]],
                       [self DCRP_STR:dic[@"buyer_dailiren"]],
                       [self DCRP_STR:dic[@"buyer_tel"]],
                       [self DCRP_STR:dic[@"buyer_accno"]],
                       [self DCRP_STR:dic[@"buyer_yinhang"]],
                       [self DCRP_STR:dic[@"buyer_shuihao"]],
                       [self DCRP_STR:dic[@"b_name"]],
                       [self DCRP_STR:dic[@"s_name"]],
                       [[self DCRP_STR:dic[@"hanshui"]] isEqual:@"1"]?@"含税":@"不含税",
                       [self DCRP_STR:dic[@"order_type"]],
                       [self DCRP_STR:dic[@"guige"]],
                       [[self DCRP_STR:dic[@"yunshu"]] isEqual:@"1"]?@"包到":@"自提",
                       [[self DCRP_STR:dic[@"baozhuang"]] isEqual:@"1"]?@"吨小包": ([[self DCRP_STR:dic[@"baozhuang"]] isEqual:@"2"]?@"小袋":@"散装"),
                       [self DCRP_STR:dic[@"number"]],
                       [self DCRP_STR:dic[@"amount"]],
                       [dic[@"note"] isEqual:@""]?@"无":@"无",
                       dic[@"yanshoubiaozhun"],
                       dic[@"fine_standard"],
                       [self DCRP_STR:dic[@"unit"]],
                       [self DCRP_STR:dic[@"damount"]],
                       [self DCRP_STR:dic[@"title"]],
                       [self DCRP_STR:dic[@"addtime"]],
                       [self DCRP_STR:dic[@"htid"]],
                       self.infoArr[0],
                       self.infoArr[1],
                       self.infoArr[2],
                       self.infoArr[3],
                       self.infoArr[4],
                       self.infoArr[6],
                       self.infoArr[5],
                       self.infoArr[7],
                       [[self DCRP_STR:dic[@"yunshu"]] isEqual:@"1"]?@"卖家":@"买家"
                       ];
    NSLog(@"循环-%@",[self DCRP_STR:dic[@"buyer_truename"]]);
    for (NSInteger i  = 0; i<34; i++) {
        NSString * str1 = [NSString stringWithFormat:@"document.getElementById('%@').innerText = '%@';",arr[i],arr2[i]];
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }
    
    NSString * payType = [self DCRP_STR:dic[@"order_type"]];
    if([payType isEqual:@"A"]){
        NSString * str1 = @"document.getElementById('c_pay_type').innerText = '全款解冻,发货全款解付';";
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }else if([payType isEqual:@"B"]){
        NSString * str1 = @"document.getElementById('c_pay_type').innerText = '全款解冻,装车分批解付';";
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }else if([payType isEqual:@"C"]){
        NSString * str1 = @"document.getElementById('c_pay_type').innerText = '全款解冻,货到分批解付';";
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }else{
        NSString * str1 = @"document.getElementById('c_pay_type').innerText = '全款解冻,完成全部订单全款解付';";
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }
    
    NSString * gg =  [THJTools ContraceGuigeTypeChangeToStrWithType:[self DCRP_STR:dic[@"guige"]]];
    NSString * ggC = [NSString stringWithFormat:@"document.getElementById('c_gg').innerText = '%@';",gg];
    [webview stringByEvaluatingJavaScriptFromString:ggC];
    NSString * orderidC = [NSString stringWithFormat:@"document.getElementById('c_number').innerText = '%@';",self.orderId];
    [webview stringByEvaluatingJavaScriptFromString:orderidC];
}
-(NSString*)DCRP_STR:(NSString*)str{
    return   [RSA decryptString:str publicKey:RSA_PubKey];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)sureBtnAction:(id)sender {
    
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定合同内容无误并且订购吗" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了按钮1，进入按钮1的事件");
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
        [self postInfo];
    }];
    [actionSheet addAction:action1];
    [actionSheet addAction:action2];
    
    
    [self presentViewController:actionSheet animated:YES completion:nil];
}
-(void)postInfo{
    [self SVHUDShow];
    NSDictionary * dic = @{
                           @"type":@"ios",
                           @"itemid":[RSA encryptString:self.orderId publicKey:RSA_PubKey],
                           @"truename":[RSA encryptString:self.infoArr[0] publicKey:RSA_PubKey],
                           @"address":[RSA encryptString:self.infoArr[1] publicKey:RSA_PubKey],
                           @"faren":[RSA encryptString:self.infoArr[2] publicKey:RSA_PubKey],
                           @"dailiren":[RSA encryptString:self.infoArr[3] publicKey:RSA_PubKey],
                           @"tel":[RSA encryptString:self.infoArr[4] publicKey:RSA_PubKey],
                           @"accno":[RSA encryptString:self.infoArr[6] publicKey:RSA_PubKey],
                           @"yinhang":[RSA encryptString:self.infoArr[5] publicKey:RSA_PubKey],
                           @"shuihao":[RSA encryptString:self.infoArr[7] publicKey:RSA_PubKey]
                           };
    NSLog(@"tijiaoLL-%@",dic);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=hetong_seller" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"卖家合同信息--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
            [self HUDShowMsg:@"下单成功!" AfterDelayDimiss:1];
            [self performSelector:@selector(popRoot) withObject:nil afterDelay:1];
        }else{
            [self HUDShowMsg:@"订购失败" AfterDelayDimiss:1];
        }
    }];
}
-(void)popRoot{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
